package com.arch.dev.client.view;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.arch.dev.client.BudgetTable;
import com.arch.dev.client.controller.BudgetController;
import com.arch.dev.client.view.chart.GChartExample01;
import com.arch.dev.client.view.chart.GChartExample02;
import com.arch.dev.client.vo.BudgetVO;
import com.arch.dev.client.vo.CatagoryVO;
import com.arch.dev.client.vo.PdfVO;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.SplitLayoutPanel;
import com.google.gwt.user.client.ui.StackLayoutPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.view.client.ListDataProvider;
import com.googlecode.gchart.client.GChart;

public class BudgetCalculatorView extends VerticalPanel {

	public BudgetCalculatorView() {

		init();

	}

	private FlexTable flexTable;

	public BudgetTable budgetTable;

	ListDataProvider<BudgetVO> dataProvider;

	SplitLayoutPanel splitPanel;

	VerticalPanel budgetVerticalPanel;

	public VerticalPanel getBudgetVerticalPanel() {

		return budgetVerticalPanel;

	}

	public void setBudgetVerticalPanel(VerticalPanel budgetVerticalPanel) {

		this.budgetVerticalPanel = budgetVerticalPanel;

	}

	private void init() {

		// horiSplitPanel = new HorizontalSplitPanel();

		// horiSplitPanel.setSplitPosition("250px");

		// horiSplitPanel.setLeftWidget(createLeftPanel());

		// horiSplitPanel.setRightWidget(createBudgetPanel());

		FlexTable ft = createFlexTable();
		ft.setWidget(0, 0, createLeftPanel());
		ft.setWidget(0, 1, createBudgetPanel());
		add(ft);

		setSize("100%", "100%");

	}

	public Widget createBudgetPanel() {

		budgetVerticalPanel = new VerticalPanel();

		budgetVerticalPanel.add(createForm());

		// VerticalPanel cellTableVP = new VerticalPanel();

		// cellTableVP.add(createCellTable());

		// cellTableVP.add(pager);

		// cellTableVP.setHorizontalAlignment(ALIGN_CENTER);

		// vp.add(cellTableVP);

		// HorizontalPanel hp = new HorizontalPanel();

		// //hp.setHorizontalAlignment(ALIGN_CENTER);

		// hp.add(pager);

		// vp.add(hp);

		budgetTable = new BudgetTable(this);
		// TestJava st = new TestJava();
		budgetVerticalPanel.add(budgetTable);

		return budgetVerticalPanel;

	}

	public BudgetTable getBudgetTable() {
		return budgetTable;
	}

	public void setBudgetTable(BudgetTable budgetTable) {
		this.budgetTable = budgetTable;
	}

	Anchor view;
	Anchor view1;
	Anchor view2;
	Anchor view3;

	public Widget createLeftPanel() {
		// Create a new stack panel

		StackLayoutPanel stackPanel = new StackLayoutPanel(Unit.EM);

		// DecoratedStackPanel stackPanel = new DecoratedStackPanel();
		stackPanel.setPixelSize(225, 800);
		//stackPanel.setSize("100%", "100%");

		VerticalPanel vp = new VerticalPanel();

		// vp.add(new HTML("<A href=''>ADD</a>"));

		view = new Anchor("View");

		/*
		 * view.addClickHandler(new ClickHandler() {
		 * 
		 * public void onClick(ClickEvent event) {
		 * 
		 * BudgetController.getInstance().getAllValues(
		 * 
		 * BudgetCalculatorView.this);
		 * 
		 * }
		 * 
		 * });
		 */
		vp.add(view);

		VerticalPanel vp1 = new VerticalPanel();

		// vp.add(new HTML("<A href=''>ADD</a>"));

		view1 = new Anchor("Graph 1");

		/*
		 * view1.addClickHandler(new ClickHandler() {
		 * 
		 * public void onClick(ClickEvent event) {
		 * 
		 * GChart gChart = getGraph1();
		 * 
		 * flexTable.setWidget(0,1,gChart.asWidget());
		 * 
		 * gChart.update();
		 * 
		 * BudgetController.getInstance().getMonthlyData(
		 * BudgetCalculatorView.this);
		 * 
		 * }
		 * 
		 * });
		 */

		vp1.add(view1);

		VerticalPanel vp2 = new VerticalPanel();

		// vp.add(new HTML("<A href=''>ADD</a>"));

		view2 = new Anchor("Graph 2");

		/*
		 * view2.addClickHandler(new ClickHandler() {
		 * 
		 * public void onClick(ClickEvent event) {
		 * 
		 * GChart gChart = getGraph2();
		 * 
		 * flexTable.setWidget(0,1,gChart.asWidget());
		 * 
		 * gChart.update();
		 * 
		 * }
		 * 
		 * });
		 */

		vp1.add(view2);

		VerticalPanel vp3 = new VerticalPanel();

		// vp.add(new HTML("<A href=''>ADD</a>"));

		view3 = new Anchor("Generate");

		/*
		 * view2.addClickHandler(new ClickHandler() {
		 * 
		 * public void onClick(ClickEvent event) {
		 * 
		 * GChart gChart = getGraph2();
		 * 
		 * flexTable.setWidget(0,1,gChart.asWidget());
		 * 
		 * gChart.update();
		 * 
		 * }
		 * 
		 * });
		 */

		vp2.add(view3);
		
		stackPanel.add(vp, "Budget", 2);

		stackPanel.add(vp1, "Graph", 2);
		stackPanel.add(vp2, "PDF", 2);

		return stackPanel;

	}

	public FlexTable createFlexTable() {

		// Create a Flex Table

		flexTable = new FlexTable();

		FlexCellFormatter cellFormatter = flexTable.getFlexCellFormatter();

		flexTable.addStyleName("cw-FlexTable");

		flexTable.setCellSpacing(5);

		flexTable.setCellPadding(3);

		// Add some text

		// cellFormatter.setHorizontalAlignment(

		// 0, 1, HasHorizontalAlignment.ALIGN_LEFT);

		// cellFormatter.setVerticalAlignment(0, 1,

		// HasVerticalAlignment.ALIGN_TOP);

		// Return the panel

		flexTable.ensureDebugId("cwFlexTable");

		return flexTable;

	}

	public void populateflexTable(List<BudgetVO> budgetVOs) {

		int rowCount = 0;

		List<BudgetVO> list = dataProvider.getList();

		list.clear();

		count = 0;

		for (BudgetVO budgetVO : budgetVOs) {

			/*
			 * 
			 * Label label1 = new Label(budgetVO.getId().toString()); Label
			 * 
			 * label2 = new Label(budgetVO.getTitle()); Label label3 = new
			 * 
			 * Label(budgetVO.getDate().toString());
			 * 
			 * flexTable.setWidget(rowCount, 0,label1);
			 * 
			 * flexTable.setWidget(rowCount, 1,label2);
			 * 
			 * flexTable.setWidget(rowCount, 2,label3);
			 */

			rowCount++;

			list.add(budgetVO);

		}

		// table.setPageSize(list.size());

		// Add the data to the data provider, which automatically pushes it to

		// the

		// widget.

	}

	CellTable<BudgetVO> table;

	SimplePager pager;

	int count;

	private Widget createCellTable() {

		// Create a CellTable.

		table = new CellTable<BudgetVO>();

		table.setWidth("100%", true);

		// Create name column.

		TextColumn<BudgetVO> nameColumn = new TextColumn<BudgetVO>() {

			@Override
			public String getValue(BudgetVO contact) {

				return contact.getTitle();

			}

		};

		// Make the name column sortable.

		nameColumn.setSortable(true);

		final DateTimeFormat dateFormat = DateTimeFormat

		.getFormat(PredefinedFormat.DATE_FULL);

		// Create address column.

		TextColumn<BudgetVO> addressColumn = new TextColumn<BudgetVO>() {

			@Override
			public String getValue(BudgetVO contact) {

				return dateFormat.format(contact.getDate());

			}

		};

		// Create name column.

		TextColumn<BudgetVO> catagoryColumn = new TextColumn<BudgetVO>() {

			@Override
			public String getValue(BudgetVO contact) {

				return contact.getCatagoryVO().getName();

			}

		};

		// Add the columns.

		// table.addColumn(rowCountColumn, "#");

		table.addColumn(nameColumn, "Description");

		table.addColumn(catagoryColumn, "Catagory");

		table.addColumn(addressColumn, "Date");

		// table.addColumn(deleteColumn, "Action");

		// Create a SimplePager.

		pager = new SimplePager();

		pager.setDisplay(table);

		dataProvider = new ListDataProvider<BudgetVO>();

		// Connect the table to the data provider.

		dataProvider.addDataDisplay(table);

		/*
		 * 
		 * 
		 * 
		 * // Add a ColumnSortEvent.ListHandler to connect sorting to the //
		 * 
		 * java.util.List. ListHandler<BudgetVO> columnSortHandler = new
		 * 
		 * ListHandler<BudgetVO>( list);
		 * 
		 * columnSortHandler.setComparator(nameColumn, new
		 * 
		 * Comparator<Tester.Contact>() { public int compare(Contact o1, Contact
		 * 
		 * o2) { if (o1 == o2) { return 0; }
		 * 
		 * 
		 * 
		 * // Compare the name columns. if (o1 != null) { return (o2 != null) ?
		 * 
		 * o1.name.compareTo(o2.name) : 1; } return -1; } });
		 * 
		 * table.addColumnSortHandler(columnSortHandler);
		 * 
		 * 
		 * 
		 * // We know that the data is sorted alphabetically by default.
		 * 
		 * table.getColumnSortList().push(nameColumn);
		 */

		// Add it to the root panel.

		// RootPanel.get().add(table);

		return table;

	}

	FlexTable layout;

	TextBox descTextBox;

	TextBox costTextBox;

	DateBox dateBox;

	Button saveButton;

	ListBox catagoryListBox;

	public Widget createForm() {

		// Create a table to layout the form options

		layout = new FlexTable();

		layout.setCellSpacing(6);

		FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();

		// Create a DateBox

		catagoryListBox = new ListBox(false);

		catagoryListBox.addChangeHandler(new ChangeHandler() {

			public void onChange(ChangeEvent event) {

				descTextBox.setText(getSelectedCatagoryName());

			}

		});

		// populateCatagoryListBox();

		DateTimeFormat dateFormat = DateTimeFormat.getLongDateFormat();

		dateBox = new DateBox();

		dateBox.setFormat(new DateBox.DefaultFormat(dateFormat));

		dateBox.setValue(new Date());

		saveButton = new Button("Save");

		saveButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {

				BudgetController.getInstance().persistValues(

				BudgetCalculatorView.this, populateVo());

			}

		});

		// Add a title to the form

		layout.setHTML(0, 0, "Enter Details");

		cellFormatter.setColSpan(0, 0, 2);

		cellFormatter.setHorizontalAlignment(0, 0,

		HasHorizontalAlignment.ALIGN_CENTER);

		descTextBox = new TextBox();

		costTextBox = new TextBox();

		// Add some standard form options

		layout.setHTML(1, 0, "Description :");

		layout.setWidget(1, 1, descTextBox);

		layout.setHTML(2, 0, "Date :");

		layout.setWidget(2, 1, dateBox);

		layout.setHTML(3, 0, "Catagory :");

		layout.setWidget(3, 1, catagoryListBox);

		layout.setHTML(4, 0, "Cost :");

		layout.setWidget(4, 1, costTextBox);

		layout.setWidget(5, 1, saveButton);

		// Wrap the content in a DecoratorPanel

		DecoratorPanel decPanel = new DecoratorPanel();

		decPanel.setWidget(layout);

		return decPanel;

	}

	private BudgetVO populateVo() {

		BudgetVO budgetVO = new BudgetVO();

		budgetVO.setTitle(descTextBox.getText());

		budgetVO.setDate(dateBox.getValue());

		CatagoryVO catagoryVO = new CatagoryVO();

		Long id = Long.parseLong(catagoryListBox.getValue(catagoryListBox
				.getSelectedIndex()));

		catagoryVO.setId(id);

		catagoryVO.setName(getSelectedCatagoryName());

		budgetVO.setCatagoryVO(catagoryVO);

		budgetVO.setCost(BigDecimal.valueOf(Double.parseDouble(costTextBox
				.getText())));

		return budgetVO;

	}

	private void populateCatagoryListBox() {

		BudgetController.getInstance().getAllCatagories(this);

	}

	public void populateCatagoryListBox(List<CatagoryVO> catagoryVOs) {

		for (CatagoryVO catagoryVO : catagoryVOs) {

			catagoryListBox.addItem(catagoryVO.getName(), catagoryVO.getId()
					.toString());

		}

		descTextBox.setText(getSelectedCatagoryName());

	}

	private String getSelectedCatagoryName() {

		return catagoryListBox.getItemText(catagoryListBox.getSelectedIndex());

	}

	GChart chart1;

	public GChart getGraph1() {

		if (chart1 == null) {

			chart1 = new GChartExample01();

		}

		return chart1;

	}

	public GChart getGraph2() {

		return new GChartExample02();

	}

	public void addPanelToRightHorizontalPanel(Widget widget) {

		flexTable.setWidget(0, 1, widget);

	}

	public void addViewListener(ClickHandler handler) {
		view.addClickHandler(handler);
	}

	public void addView1Listener(ClickHandler handler) {
		view1.addClickHandler(handler);
	}

	public void addView2Listener(ClickHandler handler) {
		view2.addClickHandler(handler);
	}
	
	public void addView3Listener(ClickHandler handler) {
		view3.addClickHandler(handler);
	}


	public void populatePdfflexTable(List<PdfVO> pdfVOs) {

		flexTable.clear();

		int rowCount = 0;

		for (PdfVO pdfVO : pdfVOs) {

			String pdfname = pdfVO.getName();

			Label label1 = new Label(pdfname);

			Label label2 = new Label(pdfVO.getDate().toString());

			Anchor link = new Anchor("Download",
					"servlet/pdf/GeneratePdf?pdfname=" + pdfname);

			flexTable.setWidget(rowCount, 0, label1);

			flexTable.setWidget(rowCount, 1, label2);

			flexTable.setWidget(rowCount, 3, link);

			rowCount++;

		}

	}

}